package com.li.shop.controller;

import com.li.shop.domain.News;
import com.li.shop.service.NewsService;
import com.li.shop.utils.SearchNewsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/page")

public class NewsController {
        @Autowired
        private NewsService newsService;

        @RequestMapping(value="/welcome",method= RequestMethod.GET)


        public String listNews(Model model){

            List<News> list = newsService.listNews();
            model.addAttribute("list", list);

           return "back/indextest";
            //return "before/introductionbark";
        }

    //王志文
    @RequestMapping(value = "/searchnews", method = RequestMethod.GET)
    public String newsPage(Model model , Integer pageSize, Integer pageNum) {
        //TODO: Add news search result
        //普通显示
        // List<news> Newss = newsService.listNews( pageSize,pageNum);
        //model.addAttribute("newss", newss);
        //return "news-list-bak";//普通页面
        //return "news-list";//adminLTE页面
        return  "/back/news-list";
    }
    //分页显示
    @GetMapping("/api/searchnews")
    @ResponseBody//需要返回json数据
    public SearchNewsResponse searchNews(Integer pageNum, Integer pageSize , String keyword){
        return  newsService.listNews(pageSize,pageNum,keyword);
    }

    @RequestMapping(value = "/addnews", method = RequestMethod.GET)
    public String addNewsPage(Model model) {
        model.addAttribute("news", new News());
        return "/back/news-add";
    }

    @GetMapping("/editnews")
    public String editNewsPage(@RequestParam("id") String id, Model model) {
        News news = newsService.findNewsByPrimaryKey(id);
        model.addAttribute("news", news);
        return "/back/news-add";
    }

        @RequestMapping(value = "/addnews", method = RequestMethod.POST)
    public String saveNews(News news) {
        int updated = newsService.saveNews(news);
        return "redirect:/news/searchnews";
    }
    
    @RequestMapping (value = "/deletenews",method=RequestMethod.GET)
    //@ResponseBody//需要返回json数据 @RequestParam("id") @PathVariable("id")
    public String deleteNews(@RequestParam("id") String id) {
        //var mymessage = confirm("确认删除嘛？");
        //if (mymessage == true) {
        int deleted = newsService.deleteNewsByPrimaryKey(id);

        // return deleted > 0;
        //return "redirect:/backadminnews/searchnews";//get请求不能用重定向
        return "/back/news-list";
        //return "redirect:/backadminnews/deletenews";
    }




}
